package cn.crm.system.service.impl;

import cn.crm.common.annotation.DataScope;
import cn.crm.common.utils.SecurityUtils;
import cn.crm.system.domain.pojo.dto.CaseDto;
import cn.crm.system.domain.pojo.dto.ScheduleCaseDTO;
import cn.crm.system.domain.pojo.dto.VisitingDto;
import cn.crm.system.domain.pojo.vo.CaseVo;
import cn.crm.system.domain.pojo.vo.ScheduleCaseVo;
import cn.crm.system.domain.pojo.vo.VisitingVo;
import cn.crm.system.mapper.CaseMapper;
import cn.crm.system.mapper.ScheduleCaseMapper;
import cn.crm.system.mapper.VisitingMapper;
import cn.crm.system.service.ICaseService;
import cn.crm.system.service.IVisitingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 处理联系人的接口
 */

@Slf4j
@Service
public class CaseServiceImpl implements ICaseService {

    @Autowired
    private CaseMapper caseMapper;
    @Autowired
    private ScheduleCaseMapper scheduleCaseMapper;

//    查询案件列表
    @Override
    @DataScope(deptAlias = "d")
    public List<CaseVo> selectCasesList(CaseDto caseDto) {
        List<CaseVo> caseVos = caseMapper.selectCasesList(caseDto);
        return caseVos;
    }
    //开始新增案件
    @Override
    public int insertcase(CaseDto caseDto) {
        //        获取到当前用户
        String createAccount = SecurityUtils.getUsername();
        //赋值给业务表里
        caseDto.setCreateBy(createAccount);
        int insertcase = caseMapper.insertcase(caseDto);
        //在进行子表添加
        int i = scheduleCaseMapper.insertscheduleCase(caseDto.getScheduleCase(), caseDto.getId());
        return insertcase+i;
    }

    @Override
    public CaseVo selectcasebyid(Long id) {
        log.debug("开始通过公司查询案件详情");

        // 查询案件基本信息
        CaseVo caseInfo = caseMapper.selectcasebyid(id);

        // 查询案件进度信息
        List<ScheduleCaseVo> scheduleCaseVos = scheduleCaseMapper.selectscheduleCasebyid(id);
        caseInfo.setScheduleCaseVos(scheduleCaseVos);

        return caseInfo;
    }

    @Override
    public int delcasebyid(Long[] id) {
        log.debug("删除案件资料");
        int delcasebyid = caseMapper.delcasebyid(id);
        //然后在删掉下面的阶段
        int i = scheduleCaseMapper.deleteScheduleCase(id);

        return delcasebyid+i;
    }

    @Override
    public int updatecase(CaseDto caseDto) {
        log.debug("开始更新案件信息");
        List<ScheduleCaseDTO> scheduleCase = caseDto.getScheduleCase();
        //拿到数组里第一对象的状态
        String scheduleState = scheduleCase.get(0).getScheduleState();
        caseDto.setSchedule(scheduleState);
        int updatecase = caseMapper.updatecase(caseDto);
        //开始修改阶段
        int i = scheduleCaseMapper.deleteScheduleCase(new Long[]{caseDto.getId()});
        int insertscheduleCase = scheduleCaseMapper.insertscheduleCase(caseDto.getScheduleCase(), caseDto.getId());
        return updatecase+insertscheduleCase;
    }

    @Override
    public List<CaseVo> selectcasebybusinessId(Long businessId) {
        log.debug("通过公司id查询出案件详情");
        List<CaseVo> caseVos = caseMapper.selectcasebybusinessId(businessId);
        return caseVos;
    }
    //查询出所有的案件名称联系人以及主要联系人
    @Override
    public List<CaseVo> selectCaseAll() {
        //查询出所有的公司名称
        List<CaseVo> caseVos = caseMapper.selectCaseAll();
        //然后查出每个公司所对应的案件是个数组
        for (CaseVo caseVo : caseVos) {
            Long businessId = caseVo.getBusinessId();
            List<CaseVo> caseVos1 = caseMapper.selectcasebybusinessId(businessId);
            caseVo.setCases(caseVos1);
        }
        return caseVos;
    }

    @Override
    public CaseVo selectcasebyidAndContacts(Long id) {
        log.debug("开始通过公司查询案件详情");

        CaseVo selectcasebyid = caseMapper.selectcasebyidAndContacts(id);
        //然后开始查状态的
        List<ScheduleCaseVo> scheduleCaseVos = scheduleCaseMapper.selectscheduleCasebyid(id);
        selectcasebyid.setScheduleCaseVos(scheduleCaseVos);
        return selectcasebyid;
    }

    @Override
    public Long selectcount() {
        Long selectcount = caseMapper.selectcount();
        return selectcount;
    }

    @Override
    public Long selectpercentage() {
        Long selectpercentage = caseMapper.selectpercentage();
        return selectpercentage;
    }

    @Override
    public Long Newadd() {
        Long newadd = caseMapper.Newadd();
        return newadd;
    }

    @Override
    public Long selectsettle() {
        Long selectsettle = caseMapper.selectsettle();
        return selectsettle;
    }

    @Override
    public Long selectcasepercentage() {
        Long selectcasepercentage = caseMapper.selectcasepercentage();
        return selectcasepercentage;
    }
}
